package com.eleven.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eleven.pojo.Comments;
import com.eleven.pojo.Msg;
import com.eleven.service.CommentsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class CommentsController {

	@Autowired
	CommentsService commentsService;
	
	public CommentsService getCommentsService() {
		return commentsService;
	}

	public void setCommentsService(CommentsService commentsService) {
		this.commentsService = commentsService;
	}



	/**
	 * 查看用户自己的评论
	 * 
	 * @param pn
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/getCommentsByUserId/{userId}",method=RequestMethod.GET)
	@ResponseBody
	public Msg getCommentsByUserId(@RequestParam(value="pn",defaultValue="1")Integer pn,@PathVariable("userId") Integer userId) {
		PageHelper.startPage(pn, 8);//传入页码和每页显示的数量
		List<Comments> commentsList = commentsService.getCommentsByUserId(userId);
		//把数据封装到PageInfo中
		//参数1：要显示的数据
		//参数2：连续查询n页的内容
		PageInfo pageInfo = new PageInfo(commentsList, 5);//5是连续显示5页的内容
		if(commentsList.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail();
		}
		
	}
	
	/**
	 * 查看项目的评论
	 * 
	 * @param pn
	 * @param proId
	 * @return
	 */
	@RequestMapping(value="/getCommentsByProId",method=RequestMethod.GET)
	@ResponseBody
	public Msg getCommentsByProId(@RequestParam(value="pn",defaultValue="1")Integer pn,@RequestParam("cmPsId") Integer cmPsId) {
		PageHelper.startPage(pn, 8);//传入页码和每页显示的数量
		List<Comments> commentsList = commentsService.getCommentsByProId(cmPsId);
		//把数据封装到PageInfo中
		//参数1：要显示的数据
		//参数2：连续查询n页的内容
		PageInfo pageInfo = new PageInfo(commentsList, 5);//5是连续显示5页的内容
		if(commentsList.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail();
		}
	}
	
	/**
	 * 查看所有评论
	 * 
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/getAllComments",method=RequestMethod.GET)
	@ResponseBody
	public Msg getAllComments(@RequestParam(value="pn",defaultValue="1")Integer pn) {
		PageHelper.startPage(pn, 10);//传入页码和每页显示的数量
		List<Comments> commentsList = commentsService.getAllComments();
		//把数据封装到PageInfo中
		//参数1：要显示的数据
		//参数2：连续查询n页的内容
		PageInfo pageInfo = new PageInfo(commentsList, 5);//5是连续显示5页的内容
		if(commentsList.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail();
		}	
	}
	
	/**
	 * 删除评论
	 * 
	 * @param cmId
	 * @return
	 */
	@RequestMapping(value="/deleteComment",method=RequestMethod.POST)
	@ResponseBody
	public Msg deleteComment(@RequestParam("cmId") Integer cmId) {
		int num = commentsService.deleteComment(cmId);
		if(num > 0) {
			return Msg.success();
		}else {
			return Msg.fail();
		}
	}
	
	/**
	 * 新增评论
	 * 
	 * @param comments
	 * @return
	 */
	@RequestMapping(value="/saveComment",method=RequestMethod.GET)
	@ResponseBody
	public Msg saveComment(@RequestParam("comment")String comment,@RequestParam("userId")Integer userId,@RequestParam("psId")Integer psId) {
		Comments comments = new Comments();
		comments.setCmUsId(userId);
		comments.setCmPsId(psId);
		comments.setCmContent(comment);
		int num = commentsService.saveComment(comments);
		if(num > 0) {
			return Msg.success();
		}else {
			return Msg.fail();
		}
			
	}
	
	/**
	 * 查找评论的详细信息
	 * 
	 * @param cmId
	 * @return
	 */
	@RequestMapping(value="/getDetailedComment/{cmId}",method=RequestMethod.GET)
	@ResponseBody
	public Msg getDetailedComment(@PathVariable("cmId") Integer cmId) {
		
		Comments comment = commentsService.getDetailedComment(cmId);
		return Msg.success().add("comment", comment);
		
	}
	
	/**
	 * 模糊查询-我的评论
	 * 
	 * @param pn
	 * @param message
	 * @param cmUsId
	 * @return
	 */
	@RequestMapping(value="/searchCommentWithUserId",method=RequestMethod.GET)
	@ResponseBody
	public Msg searchCommentWithUserId(@RequestParam(value="pn",defaultValue="1")Integer pn,@RequestParam("condition")String condition,@RequestParam("cmUsId")Integer cmUsId) {
		PageHelper.startPage(pn, 8);//传入页码和每页显示的数量
		List<Comments> commentsList = commentsService.getMyCommentsByCondition(condition,cmUsId);
		//把数据封装到PageInfo中
		//参数1：要显示的数据
		//参数2：连续查询n页的内容
		PageInfo pageInfo = new PageInfo(commentsList, 5);//5是连续显示5页的内容
		if(commentsList.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail();
		}
		
	}
	
	/**
	 * 模糊查询-所有项目评论
	 * 
	 * @param pn
	 * @param condition
	 * @return
	 */
	@RequestMapping(value="/searchCommentWithAll",method=RequestMethod.GET)
	@ResponseBody
	public Msg searchCommentWithAll(@RequestParam(value="pn",defaultValue="1")Integer pn,@RequestParam("condition")String condition) {
		PageHelper.startPage(pn, 8);//传入页码和每页显示的数量
		List<Comments> commentsList = commentsService.getAllCommentsByCondition(condition);
		//把数据封装到PageInfo中
		//参数1：要显示的数据
		//参数2：连续查询n页的内容
		PageInfo pageInfo = new PageInfo(commentsList, 5);//5是连续显示5页的内容
		if(commentsList.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail();
		}
		
	}
	
	/**
	 * 模糊查询-某个项目评论
	 * 
	 * @param pn
	 * @param condition
	 * @return
	 */
	@RequestMapping(value="/searchCommentWithPsId",method=RequestMethod.GET)
	@ResponseBody
	public Msg searchCommentWithPsId(@RequestParam(value="pn",defaultValue="1")Integer pn,@RequestParam("condition")String condition,@RequestParam("cmPsId")Integer cmPsId) {
		PageHelper.startPage(pn, 8);//传入页码和每页显示的数量
		List<Comments> commentsList = commentsService.getProCommentsByCondition(condition,cmPsId);
		//把数据封装到PageInfo中
		//参数1：要显示的数据
		//参数2：连续查询n页的内容
		PageInfo pageInfo = new PageInfo(commentsList, 5);//5是连续显示5页的内容
		if(commentsList.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail();
		}
	}
	
	
}
